import { ref, onMounted } from 'vue';
import useService from '@/hooks/useService';

export function useFeeHook(config: { storeId?: string;}) {
    const uniqueKey = '费用主体定义';
    const feemain = ref([]);
    const loaded = ref(false);
    const service = useService({ prefix: "bcApi" });
    const feeMainItems = ref<{
        value: any;
        title: string;
    }[]>([]);

    const initMode = async () => {
        const result = await service.request<string, any[]>(uniqueKey, 'FetchFeeMain', '');
        if (result.length > 0) {
            feemain.value = result;
            feeMainItems.value = result.map(x => ({
                title: x.c_name,
                value: x.c_fee_main
            }));
        }
    };

    const GetFeeMainItems = async (storeId?: string) => {
        const result = await service.request<string, any[]>(uniqueKey, 'FetchFeeMain', storeId || '');
        if (result.length > 0) {
            return result.map(b => ({ title: b.c_name, value: b.c_fee_main }));
        }
        return [];
    };

    onMounted(() => {
        if (!loaded.value) {
            loaded.value = true;
            initMode();
        }
    });

    
    return {
        feeMainItems,
        GetFeeMainItems
    };
}

